﻿using UnityEngine;
using System.Collections;

public class NetworkKit : MonoBehaviour
{
    private static NetworkKit instance;

    void Awake()
    {
        instance = this;
    }

    public static NetworkKit GetInstance()
    {
        return instance;
    }
    //请求
    public void Requst(string url, System.Action<string> acceptURLHandler, System.Action failedHandler)
    {
        StartCoroutine(GetRequest(url, null, acceptURLHandler, failedHandler));
    }

    //携程加载数据
    IEnumerator GetRequest(string url, WWWForm form, System.Action<string> acceptURLHandler, System.Action failedHandler)
    {
        WWW www;
        if (form == null)       
        {
            www = new WWW(url);     //get请求
        }
        else
        {
            www = new WWW(url, form);       //post请求
        }

        //等待数据加载完毕
        while (!www.isDone)
        {
            yield return null;
        }
        //成功加载完毕之后的回调方法
        acceptURLHandler(www.text);
    }
}
